/*******************************************************************************
 *
 * Copyright (c) 2008-2011 ayound@gmail.com
 * This program and the accompanying materials
 * are made available under the terms of the GNU GENERAL PUBLIC LICENSE
 * which accompanies this distribution, and is available at
 * http://dev.perl.org/licenses/gpl1.html
 * All rights reserved.
 *
 * Created on 2009-11-10
 *******************************************************************************/

package org.ayound.skin.editor.core;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.image.RGBImageFilter;

public class ColorFillFilter extends RGBImageFilter implements IColorConvert{

	/**
	 * Description of the Field
	 */
	private int red = 0, green = 0, blue = 0;

	public int getBlue() {
		return blue;
	}

	public int getGreen() {
		return green;
	}

	public int getRed() {
		return red;
	}

	/**
	 * Constructor for the ColorFillFilter object
	 *
	 * @param r
	 *            Description of Parameter
	 * @param g
	 *            Description of Parameter
	 * @param b
	 *            Description of Parameter
	 */
	public ColorFillFilter(int r, int g, int b) {
		setColor(r, g, b);
		canFilterIndexColorModel = true;
	}


	/**
	 * Constructor for the ColorFillFilter object
	 */
	public ColorFillFilter() {
		this(0, 0, 0);
	}

	/**
	 * Sets the Color attribute of the ColorFillFilter object
	 *
	 * @param r
	 *            The new Color value
	 * @param g
	 *            The new Color value
	 * @param b
	 *            The new Color value
	 */
	public void setColor(int r, int g, int b) {
		red = r;
		green = g;
		blue = b;
		if (false && red == 100 && green == 100 && blue == 100) {
			red = 90;
			green = 90;
			blue = 90;
		}
	}

	/**
	 * Sets the Red attribute of the ColorFillFilter object
	 *
	 * @param r
	 *            The new Red value
	 */
	public void setRed(int r) {
		red = r;
	}

	/**
	 * Sets the Green attribute of the ColorFillFilter object
	 *
	 * @param g
	 *            The new Green value
	 */
	public void setGreen(int g) {
		green = g;
	}

	/**
	 * Sets the Blue attribute of the ColorFillFilter object
	 *
	 * @param b
	 *            The new Blue value
	 */
	public void setBlue(int b) {
		blue = b;
	}

	/**
	 * Description of the Method
	 *
	 * @param x
	 *            Description of Parameter
	 * @param y
	 *            Description of Parameter
	 * @param rgb
	 *            Description of Parameter
	 * @return Description of the Returned Value
	 */
	public int filterRGB(int x, int y, int rgb) {
		if (rgb == 0 || rgb == ImageUtils.TRANSPARENT_PIXEL) {
			return rgb;
		}

		int alpha = (rgb >> 24) & 0xff;
		int r = ((rgb & 0x00ff0000) >> 16) + red;
		if (r < 0) {
			r = 0;
		}
		if (r > 255) {
			r = 255;
		}
		int g = ((rgb & 0x0000ff00) >> 8) + green;
		if (g < 0) {
			g = 0;
		}
		if (g > 255) {
			g = 255;
		}
		int b = ((rgb & 0x000000ff))  + blue;
		if (b < 0) {
			b = 0;
		}
		if (b > 255) {
			b = 255;
		}
		Color newColor = new Color(r,g,b);
		return (newColor.getRGB() & 0x00ffffff) | (alpha << 24);
	}

	public void filter(BufferedImage src, BufferedImage dest) {
		for (int x = 0, c = src.getWidth(); x < c; x++) {
			for (int y = 0, d = src.getHeight(); y < d; y++) {
				dest.setRGB(x, y, filterRGB(x, y, src.getRGB(x, y)));
			}
		}
	}

	public Color convert(Color color) {
		return new Color(filterRGB(0, 0, color.getRGB()));
	}
}
